import $ from 'jQuery';
let overAllIds = new Array();  //列表选中的全局数组
(function () {
    let $vue = new Vue({
        el: '.operateLogs-main-hook',
        data: {
            boxs: {
                box1: {show: true, hook: '.box1-hook', lock: true, url: '', params: {}},
                box2: {show: false, hook: '.box2-hook', url: '', params: {}},
                box3: {show: false, hook: '.box3-hook', url: '', params: {}},
                box4: {show: false, hook: '.box4-hook', url: '', params: {}}
            },
            logType: []
        },
        created: function () {
            IOT.getServerData(URI.LOGS.OPERATE.TYPE, {palletId: 1}, (ret) => {//获取日志类型
                if (ret.code === 200) {
                    this.logType = $.extend({}, this.logType, ret.data);
                } else {
                    IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                }
            });
        },
        methods: {
            emitPage: function () {
                M.Page.load(this.boxs);
            },
            // 上一页
            prePage: function () {
                M.Page.prePage(this.boxs);
            },
            // 触发刷新页(当前显示的页面)
            refreshPage: function () {
                M.Page.refreshPage(this.boxs);
            },
            // 清理日志
            clearLogs: function () {
                let operateClear = overAllIds;
                if(operateClear.length == 0){
                    layer.alert('请选择一条数据');
                    return false;
                }else {
                    layer.confirm('&nbsp;&nbsp; &nbsp;&nbsp;你确定要清理日志吗？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        let params = {};
                        params.logId = operateClear.map(function (item,index,input) {
                            return item.logId;
                        });
                        IOT.showOverlay('清理中，请稍等...');
                        IOT.getServerData(URI.LOGS.OPERATE.CLEAR, params, (ret) => {
                            IOT.hideOverlay();
                            if (ret && ret.code === 200) {
                                IOT.tips('清理成功！', 'success');
                                layer.closeAll();
                                M.Table.refresh.all();
                            } else {
                                IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                            }
                        });
                    }, function(){
                        //取消
                    });
                }
            }
        },
        mounted: function () {
            this.$nextTick(() => {
                let tableHookName = '.operateLogs-table-hook';
                let $accountBox = $('.operateLogs-hook');
                //初始化时间插件
                modal.initDate("startdate","enddate");
                var $form = $accountBox.find('.form-hook');
                // 查询
                $form.off('submit').on('submit', function () {
                    $(tableHookName).bootstrapTable('onCustomSearch');
                    return false;
                });
                var $tableHook = $accountBox.find(tableHookName).bootstrapTable({
                    scrollbar: 'operateLogs-table-body',
                    striped: true, // 设置为 true 会有隔行变色效果
                    pagination: true, // true 显示分页
                    paginationDetail: true, // 分页详情
                    sidePagination: 'server', // 服务器端
                    method: 'post',
                    url: URI.LOGS.OPERATE.LIST,
                    debug: false,
                    cache: false,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 100],
                    // fixedColumns: true,
                    customButton: '',
                    customQueryParams: function (params) {
                        var formParams = $form.serializeJson();
                        var queryParams = $.extend({}, params, formParams);
                        //console.log(queryParams);
                        return queryParams;
                    },
                    onLoadSuccess: function (data) {
                        //console.log(JSON.stringify(data));
                    },
                    onLoadError: function (status, xhr) {
                    },
                    onCheckAll: function (rows) {
                        
                    },
                    columns: [
                        {
                            field: 'checkStatus', checkbox: true, align: 'center', fixedLeft: true, width: 60,
                            formatter: function (i,row) {//每次加载checkbox时判断当前row的palletId是否已经存在全局里
                                for(var k=0;k<overAllIds.length;k++){
                                    //全局变量中含有id，则该复选框选中
                                    if(overAllIds[k].logId == row.logId){
                                        return {
                                            checked : true //存在则选中
                                        }
                                    }
                                }
                            }
                        },
                        {
                            field: 'userName', title: '用户名称', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'logTypeStr', title: '日志类型', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'logNote', title: '日志描述', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'logTime', title: '操作时间', align: 'center', fixedLeft: true,tips:true,
                            formatter: function (val, row, index) {
                                return val;
                            }
                        },
                        {
                            field: 'logId', title: '操作IP', align: 'center', fixedLeft: true,tips:true
                        }
                    ]
                });
                //表格结束
                $('.operateLogs-table-hook').on('uncheck.bs.table check.bs.table check-all.bs.table uncheck-all.bs.table',function(e,rows){
                    var data = $.isArray(rows) ? rows : [rows]; //点击时获取选中的行或取消选中的行
                    examine(e.type,data); //保存到全局里
                });
                //权限——查看
                if (!M.Authority.checkAuthority('account-detail')&&!M.Authority.checkAuthority('account-modify')) {
                    $(tableHookName).bootstrapTable('hideColumn', 'operate');//隐藏列
                }
            });
        }
    });
})();

function examine(type,data){//操作选中的行
    if(type.indexOf('uncheck')==-1){
        $.each(data,function(i,v){//如果是选中则添加选中行的数据
            //添加时，判断一行或多行的palletId是否已经在数组里，不存则添加　
            overAllIds.indexOf(v.logId) == -1 ? overAllIds.push(v) : -1;
        });
    }else{
        $.each(data,function(i,v){
            for(var k = overAllIds.length-1;k>=0;k--){
                if(overAllIds[k].logId == v.logId)
                    overAllIds.splice(k,1);//删除取消选中行
            }
        });
    }
}